package com.dengpp.pdform.base;


import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dengpp.pdform.R;
import com.dengpp.pdform.adapter.AttributeAdapter;
import com.dengpp.pdform.config.AttributeConfig;


public abstract class BaseAttributeViewHolder extends RecyclerView.ViewHolder {
    public BaseAttributeViewHolder(@NonNull View itemView) {
        super(itemView);
    }

    public abstract void bind(AttributeConfig attribute, boolean isEditMode,
                              AttributeAdapter.OnAttributeChangeListener listener);


    /**
     * 当 ViewHolder 被回收时调用，用于清理资源
     */
    public void onViewRecycled() {
        // 子类可以重写此方法来清理资源
    }

    /**
     * 生成带必填星号的标题
     *
     * @param title      原始标题
     * @param isRequired 是否为必填项
     * @return 处理后的标题（带红色星号）
     */
    protected CharSequence getTitleWithRequiredStar(String title, boolean isRequired) {
        if (isRequired) {
            String titleWithStar = title + " *";
            SpannableString spannableString = new SpannableString(titleWithStar);
            // 设置星号为红色
            ForegroundColorSpan redSpan = new ForegroundColorSpan(
                    itemView.getContext().getResources().getColor(R.color.library_required_star)
            );
            spannableString.setSpan(redSpan, title.length(), titleWithStar.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return spannableString;
        } else {
            return title;
        }
    }
}
